package com.alone.wchat.admin.system.controller;

import com.alone.wchat.admin.system.base.AbstractController;
import com.alone.wchat.admin.system.entity.SysUserEntity;
import com.alone.wchat.admin.system.service.SysUserRoleService;
import com.alone.wchat.admin.system.service.SysUserService;
import com.alone.wchat.admin.utils.ShiroUtils;
import com.alone.wchat.common.utils.PageUtils;
import com.alone.wchat.common.utils.R;
import com.alone.wchat.common.validator.Assert;
import com.alone.wchat.common.validator.ValidatorUtils;
import com.alone.wchat.common.validator.group.AddGroup;
import com.alone.wchat.common.validator.group.UpdateGroup;
import net.dreamlu.boot.annotation.SysLog;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * created by weirdor on 2018/5/1
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController  extends AbstractController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysUserService.queryPage(params);

        return R.ok().put("data", page.getList());
    }

    /**
     * 获取登录的用户信息
     */
    @RequestMapping("/info")
    public R info(){

        return R.ok().put("data",getUser());
    }

    /**
     * 修改登录用户密码
     */
    @RequestMapping("/password")
    public R password(String oldPassword, String password){
        Assert.isBlank(password, "新密码不为能空");

        //原密码
        oldPassword = new Sha256Hash(oldPassword, getUser().getSalt()).toHex();
        //新密码
        password = new Sha256Hash(password, getUser().getSalt()).toHex();
        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), oldPassword, password);
        if(!flag){
            return R.error("原密码不正确");
        }

        return R.ok().put("status",1);
    }

    /**
     * 用户信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId){
        SysUserEntity user = sysUserService.selectById(userId);

        //获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return R.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @RequestMapping("/save")
    public R save(@RequestBody SysUserEntity user){
        ValidatorUtils.validateEntity(user, AddGroup.class);

        sysUserService.save(user);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @RequestMapping("/update")
    public R update(@RequestBody SysUserEntity user){
        ValidatorUtils.validateEntity(user, UpdateGroup.class);

        sysUserService.update(user);

        return R.ok();
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] userIds){
        if(ArrayUtils.contains(userIds, 1L)){
            return R.error("系统管理员不能删除");
        }

        if(ArrayUtils.contains(userIds, getUserId())){
            return R.error("当前用户不能删除");
        }

        sysUserService.deleteBatchIds(Arrays.asList(userIds));

        return R.ok();
    }
}
